Skip to content

chore: add script to fix invalid proposal's scores_by_strategy #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

wa0x6e
Copy link
Contributor

@wa0x6e wa0x6e commented Aug 7, 2025

Fix https://github.com/snapshot-labs/workflow/issues/620
See discussion in https://discord.com/channels/955773041898573854/1400932129378013255

This PR will add a script to cleanup the invalid format in the scores_by_strategy column, for some proposals using weighted and quadratic voting type.

There are ~2300 concerned proposals, all created more than 3 years ago (recent proposals don't have this issue)

The invalid format is [[[0]]] instead of [[0]] (there is one extra array depth), which is invalid according to the tests

The scripts had mostly been written by claude, but tested and fixed by an human.

You can find all the proposals with invalid scores with

SELECT
	id,
	SPACE,
	scores_by_strategy,
    type,
	JSON_DEPTH(scores_by_strategy) AS depth
FROM
	proposals
WHERE
	JSON_DEPTH(scores_by_strategy) > 3 ORDER BY
	JSON_DEPTH(scores_by_strategy) ASC
LIMIT
	100;

Run

Run a preview to show only the buggy data (no update)

yarn ts-node scripts/fix-scores-by-strategy.ts preview

Run the script to fix the data

yarn ts-node scripts/fix-scores-by-strategy.ts run

@wa0x6e wa0x6e requested a review from Copilot August 7, 2025 23:57
@wa0x6e wa0x6e changed the title chore: add script to fix invalid proposal's scores_by_strategy chore: add script to fix invalid proposal's scores_by_strategy Aug 7, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a cleanup script to fix invalid scores_by_strategy data format in proposals with weighted and quadratic voting types. The issue affects approximately 2300 proposals created over 3 years ago where the data has an extra array depth level ([[[0]]] instead of [[0]]).

  • Adds a new script that identifies and fixes malformed JSON data in the scores_by_strategy column
  • Provides both preview and run modes for safe execution
  • Includes comprehensive logging and error handling for the data migration process


if (isBuggy) {
// Fix the data by flattening: [[[0.75],[0]]] becomes [[0.75,0]]
const fixed = scoresByStrategy.map(item => item.flat());
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using flat() without a depth parameter only flattens one level deep. If the nested structure is deeper than expected (e.g., [[[[0]]]]), this won't fully fix the issue. Consider using flat(Infinity) or specify the expected depth.

Suggested change
const fixed = scoresByStrategy.map(item => item.flat());
const fixed = scoresByStrategy.map(item => item.flat(Infinity));

Copilot uses AI. Check for mistakes.

type: proposal.type,
original: scoresByStrategy,
fixed: fixed
});
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buggy data detection logic assumes all sub-items should be arrays, but this may incorrectly flag valid mixed data structures. Consider adding validation that the flattened result produces the expected format before marking as buggy.

Suggested change
});
let fixed: any = null;
if (looksBuggy) {
// Fix the data by flattening: [[[0.75],[0]]] becomes [[0.75,0]]
fixed = scoresByStrategy.map(item => item.flat());
// Only mark as buggy if the fixed data is in the expected format
if (isArrayOfArraysOfNumbers(fixed)) {
fixCandidates.push({
id: proposal.id,
type: proposal.type,
original: scoresByStrategy,
fixed: fixed
});
}

Copilot uses AI. Check for mistakes.

@wa0x6e wa0x6e requested a review from bonustrack August 8, 2025 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant